{% extends 'layout.html' %}


{% block content %}
    <div class="container">
        <div class="panel panel-default">
            <div class="panel-heading">表单</div>
            <div class="panel-body">
                <form id="FormAdd">
                    <div class="clearfix">
                        {% for field in form %}
                            <div class="col-xs-6">
                                <div class="form-group" style="position: relative; margin-bottom: 20px">
                                    <label>{{ field.label }}</label>
                                    {{ field }}
                                    <span class="error-msg" style="color: red; position: absolute"></span>
                                </div>
                            </div>
                        {% endfor %}
                        <div class="col-xs-12">
                            <button id="butAdd" type="button" class="btn btn-primary">提 交</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
        <div class="panel panel-default">
            <div class="panel-heading">
                <span class="glyphicon glyphicon-th-list" aria-hidden="true"></span>
                任务列表
            </div>
            <table class="table table-bordered">
                <thead>
                <tr>
                    <th>ID</th>
                    <th>标题</th>
                    <th>级别</th>
                    <th>负责人</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                {% for obj in queryset %}
                    <tr>
                        <th>{{ obj.id }}</th>
                        <th>{{ obj.title }}</th>
                        <th>{{ obj.get_level_display }}</th>
                        <th>{{ obj.user.username }}</th>
                        <td>
                            <a class="btn btn-primary btn-xs" href="#">修改</a>
                            <a class="btn btn-danger btn-xs" href="#">删除</a>
                        </td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>
        </div>
        <ul class="pagination" style="float: left">
            {{ page_str }}
        </ul>
    </div>
{% endblock %}


{% block js %}
    <script type="text/javascript">
        $(function () {
            //页面加载完成后执行
            binBtn1Event();
        });

        function binBtn1Event() {
            $('#butAdd').click(function () {
                //清空错误信息
                $('.error-msg').empty();
                $.ajax({
                    url: "/task/add/",
                    type: "post",
                    data: $('#FormAdd').serialize(),
                    dataType: "JSON",
                    success: function (res) {
                        if (res.status) {
                            //刷新页面
                            location.reload();
                        } else {
                            console.log(res.error);
                            $.each(res.error, function (name, data) {
                                console.log(name, data);
                                //显示错误信息
                                $('#id_' + name).next().text(data[0]);
                            })
                        }
                    }
                })
            })
        }
    </script>
{% endblock %}